package com.yanqu.road.server.cmd.activity.springunionbanquet;

import com.yanqu.road.entity.activity.springunionbanquet.config.SpringUnionBanquetConfig;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SpringUnionBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.springunionbanquet.SpringUnionBanquetMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;

@Cmd(code = Protocol.S_SPRING_UNION_BANQUET_UNION_TOP, desc = "商会top")
public class SpringUnionBanquetUnionTopCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        SpringUnionBanquetConfig config = SpringUnionBanquetMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        if(!SystemOpenMgr.systemOpen(player, eSystemId.SpringBanquet.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        SpringUnionBanquetProto.CrossSpringUnionBanquetUnionTopReqMsg.Builder crossMsg = SpringUnionBanquetProto.CrossSpringUnionBanquetUnionTopReqMsg.newBuilder();
        crossMsg.setActivityId(config.getActivityId());
        crossMsg.setUnionUid(unionUid);
        for (Map.Entry<Long, CrossUnionMember> entry : CrossUnionMgr.getUnionMemberMap(unionUid).entrySet()) {
            crossMsg.addMemberId(entry.getKey());
        }
        player.sendPacket(Protocol.C_SPRING_UNION_BANQUET_UNION_TOP, crossMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SPRING_UNION_BANQUET_UNION_TOP, SpringUnionBanquetProto.SpringUnionBanquetUnionTopRespMsg.newBuilder().setRet(ret));
        }
    }
}
